package com.project.hqshorttaxi.service.impl;

import com.project.hqshorttaxi.dto.LogParam;
import com.project.hqshorttaxi.entity.Log;
import com.project.hqshorttaxi.mapper.LogMapper;
import com.project.hqshorttaxi.service.LogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LogServiceImpl implements LogService {

    @Resource
    private LogMapper logMapper;

    @Override
    public Log getById(Integer id) {
        return logMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(Log entity) throws Exception {
        logMapper.insertSelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Log entity) throws Exception {
        logMapper.updateByPrimaryKeySelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Integer id) throws Exception {
        logMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Log> list(LogParam param) {
        Example example = new Example(Log.class);
        Example.Criteria c = example.createCriteria();
        if (param.getLogType() != null) {
            c.andEqualTo("logType", param.getLogType());
        }
        if (!StringUtils.isEmpty(param.getLogName())) {
            c.andLike("logName", "%" + param.getLogName() + "%");
        }
        if (param.getBeginTime() != null) {
            c.andGreaterThanOrEqualTo("logTime", param.getBeginTime());
        }
        if (param.getEndTime() != null) {
            c.andLessThanOrEqualTo("logTime", param.getEndTime());
        }
        example.orderBy("logTime").desc();
        List<Log> list = logMapper.selectByExample(example);
        return list;
    }
}
